﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.IO;

namespace KKSGameLibrary.Logic
{
    public class CardSet
    {
        public CardSet()
        {
            InitCardSet();
        }

        List<Card> cards = null;
        public void InitCardSet()
        {
            //Stream stream = null;
            //var stream = Application.GetResourceStream(new Uri("CardSetData.xml", UriKind.Relative)).Stream;
            //DataContractSerializer serializer = new DataContractSerializer(typeof(List<Card>));
            //cards = serializer.ReadObject(stream) as List<Card>;
            cards = new List<Card>();
            AddCard(0x00000000, 0x00000001, 4);
            AddCard(0x00000000, 0x01000100, 4);
            AddCard(0x00000000, 0x01010000, 4);
            AddCard(0x00000000, 0x01020300, 4);
            AddCard(0x00000000, 0x01020304, 4);
            AddCard(0x01000000, 0x00000000, 4);
            AddCard(0x01000000, 0x00010001, 4);
            AddCard(0x01000100, 0x00000000, 4);
            AddCard(0x01000200, 0x00000000, 4);
            AddCard(0x01010000, 0x00000000, 4);
            AddCard(0x01010000, 0x00000101, 4);
            AddCard(0x01010100, 0x00000000, 4);
            AddCard(0x01010101, 0x00000000, 4);
            AddCard(0x01000000, 0x00010100, 4);
            AddCard(0x01000000, 0x00000101, 4);
            AddCard(0x01000000, 0x00010203, 4);
            AddCard(0x01020000, 0x00000000, 4);
            AddCard(0x01010100, 0x00000001, 4);


            cards.Add(new Card
            {
                CardType = CardType.Church
            });
        }

        void AddCard(int castledata, int roaddata, int count)
        {
            for (int i = 0; i < count; i++)
            {
                CardType type = CardType.Empty;
                if (castledata != 0)
                    type |= CardType.Castle;
                if (roaddata != 0)
                    type |= CardType.Road;

                cards.Add(new Card
                {
                    CardType = type,
                    CastleData = castledata,
                    RoadData = roaddata,
                });
            }
        }

        public int Count
        {
            get
            {
                return cards.Count;
            }
        }

        Random rand = new Random();
        bool firstcard = true;
        public Card GetCard()
        {
            if (cards.Count == 0)
                return null;

            // get card index
            int index = 0;
            if(firstcard)
            {
                firstcard = false;
            }else
                index = rand.Next(cards.Count);

            var retcard = cards[index];
            cards.RemoveAt(index);

            return retcard;

            //var card = cards[0];
            //cards.RemoveAt(0);
            //return card;
        }
    }
}
